package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.AskForleave;
import tgc.edu.exam.entity.Student;
import tgc.edu.exam.entity.StudentInformation;
import tgc.edu.exam.entity.SysRole;
import tgc.edu.exam.entity.SysUser;
import tgc.edu.exam.entity.TbClass;
import tgc.edu.exam.entity.Teacher;
import tgc.edu.exam.security.UserUtils;
import tgc.edu.exam.service.AskForleaveService;
import tgc.edu.exam.service.SysFileService;
import tgc.edu.exam.service.TbClassService;
import tgc.edu.exam.web.form.AskForleaveForm;

@Controller
@RequestMapping(value = "/askForleave")
public class AskForleaveController extends CommonController<AskForleave, UUID, AskForleaveForm> {

	@Autowired
	private AskForleaveService service;
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private UserUtils userUtils;

	@RequestMapping(value = "/details")
	public void details(String id, ModelMap map) {
		map.put("id", id);
	}

	@RequestMapping(value = "/instant")
	@ResponseBody
	public Object instant() {
		String msg = "数据出错！请联系管理员";
		Specification<AskForleave> spec = buildSpec1();
		List<AskForleave> students = service.findAll(spec);
		if (students.size() != 0) {
			SysUser teacher = userUtils.getUser();
			for (AskForleave askForleave : students) {
				TbClass tbClass = tbClassService.findByName(askForleave.getTbClass());
				if (teacher != null) {
					boolean ishas = false;
					List<SysRole> roles = teacher.getRoles();
					for (SysRole sysRole : roles) {
						List<SysRole> children = sysRole.getChildren();
						for (SysRole sysRole2 : children) {
							String code = "ROLE_QJSHGLY";
							String rolecode = sysRole2.getCode();
							if (code.equals(rolecode)) {
								ishas = true;
							}
						}
					}
					if (ishas) {
						askForleave.setState(2);
						service.save(askForleave);
					} else {

						if (tbClass.getCtype().getType().equals("专题班")) {
							askForleave.setState(2);
							service.save(askForleave);
						} else {
							askForleave.setState(4);
							service.save(askForleave);
						}
					}
				} else {

					if (tbClass.getCtype().getType().equals("专题班")) {
						askForleave.setState(2);
						service.save(askForleave);
					} else {
						askForleave.setState(4);
						service.save(askForleave);
					}
				}
			}
			msg = "操作完成！";
		} else {
			msg = "没有要审核的数据哟！";
		}
		return msg;
	}

	@RequestMapping(value = "/agree")
	@ResponseBody
	public Object agree(String id) {
		String msg = "数据出错！请联系管理员";
		AskForleave askForleave = service.findById(UUID.fromString(id));
		TbClass tbClass = tbClassService.findByName(askForleave.getTbClass());
		if (askForleave != null) {
			SysUser teacher = userUtils.getUser();
			if (teacher != null) {
				boolean ishas = false;
				List<SysRole> roles = teacher.getRoles();
				for (SysRole sysRole : roles) {
					List<SysRole> children = sysRole.getChildren();
					for (SysRole sysRole2 : children) {
						String code = "ROLE_QJSHGLY";
						String rolecode = sysRole2.getCode();
						if (code.equals(rolecode)) {
							ishas = true;
						}
					}
				}
				if (ishas) {
					askForleave.setState(2);
					service.save(askForleave);
					msg = "操作成功！";
				} else {

					if (tbClass.getCtype().getType().equals("专题班")) {
						askForleave.setState(2);
						service.save(askForleave);
						msg = "操作成功！";
					} else {
						askForleave.setState(4);
						service.save(askForleave);
						msg = "操作成功！待管理员审核...";
					}
				}
			} else {

				if (tbClass.getCtype().getType().equals("专题班")) {
					askForleave.setState(2);
					service.save(askForleave);
					msg = "操作成功！";
				} else {
					askForleave.setState(4);
					service.save(askForleave);
					msg = "操作成功！待管理员审核...";
				}
			}
		}
		return msg;
	}

	@RequestMapping(value = "/refuse")
	@ResponseBody
	public Object refuse(String id, String reason) {
		String msg = "数据出错！请联系管理员";
		AskForleave askForleave = service.findById(UUID.fromString(id));
		if (askForleave != null) {
			askForleave.setState(3);
			askForleave.setReason(reason);
			service.save(askForleave);
			msg = "操作成功！";
		}
		return msg;
	}

	private Specification<AskForleave> buildSpec1() {
		SysUser teacher = userUtils.getUser();

		Specification<AskForleave> spec = new Specification<AskForleave>() {

			@Override
			public Predicate toPredicate(Root<AskForleave> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if (teacher != null) {
					boolean ishas = false;
					boolean ishas1 = false;
					List<SysRole> roles = teacher.getRoles();
					for (SysRole sysRole : roles) {
						List<SysRole> children = sysRole.getChildren();
						for (SysRole sysRole2 : children) {
							String code = "ROLE_QJSHGLY";
							String code1 = "ROLE_QJSHBZR";
							String rolecode = sysRole2.getCode();
							if (code.equals(rolecode)) {
								ishas = true;
							}
							if (code1.equals(rolecode)) {
								ishas1 = true;
							}
						}
					}
					if (ishas) {
						rules.add(cb.equal(root.get("state"), 4));
						if(ishas1) {
							rules.add(cb.equal(root.get("state"), 1));
						}
					} else {
						List<TbClass> list = tbClassService.findByTeacherIdUsername(teacher.getUsername());
						for (TbClass tbClass : list) {
							rules.add(cb.equal(root.get("tbClass"), tbClass.getName()));
						}
						rules.add(cb.equal(root.get("state"), 1));
					}
				} else {
					rules.add(cb.equal(root.get("state"), 1));
				}
				return cb.or(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	@Override
	public Specification<AskForleave> buildSpec(AskForleaveForm form) {
		SysUser teacher = userUtils.getUser();

		Specification<AskForleave> spec = new Specification<AskForleave>() {

			@Override
			public Predicate toPredicate(Root<AskForleave> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if (teacher != null) {
					boolean ishas = false;
					boolean ishas1 = false;
					List<SysRole> roles = teacher.getRoles();
					for (SysRole sysRole : roles) {
						List<SysRole> children = sysRole.getChildren();
						for (SysRole sysRole2 : children) {
							String code = "ROLE_QJSHGLY";
							String code1 = "ROLE_QJSHBZR";
							String rolecode = sysRole2.getCode();
							if (code.equals(rolecode)) {
								ishas = true;
							}
							if (code1.equals(rolecode)) {
								ishas1 = true;
							}
						}
					}
					if (ishas) {
						rules.add(cb.equal(root.get("state"), 4));
						if(ishas1) {
							rules.add(cb.equal(root.get("state"), 1));
						}
					} else {
						List<TbClass> list = tbClassService.findByTeacherIdUsername(teacher.getUsername());
						for (TbClass tbClass : list) {
							rules.add(cb.equal(root.get("tbClass"), tbClass.getName()));
						}
						rules.add(cb.equal(root.get("state"), 1));
					}
				} else {
					rules.add(cb.equal(root.get("state"), 1));
				}
				return cb.or(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

}
